/**
 * Copyright (C) 2010 Hamburg Sud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hamburgsud.log4testing.core.configuration;

import java.net.URL;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

public class Log4TestingConfiguration {

	final private static Logger logger = Logger
			.getLogger(Log4TestingConfiguration.class);

	private static Document config = null;

	final public static String LOG4_TESTING_CONFIG_FILE = "log4testing.xml";

	public static Document getConfig() {
		if (config == null) {
			try {
				final SAXReader reader = new SAXReader();
				final URL configLocation = ClassLoader
						.getSystemResource(LOG4_TESTING_CONFIG_FILE);
				config = reader.read(configLocation);
			} catch (DocumentException e) {
				// When the configuration file cannot be loaded log4testing is
				// not runnable. That's the reason why we print the stack trace
				// out to the error stream.
				logger.error("The configuration file of "
						+ "log4testing could not be loaded.", e);
			}
		}
		return config;
	}

}
